<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DayOfYear(d)</title>
</head>
<body>
  <script>
    var d = prompt("请输入日期：");

    function DayOfYear(d) {
      var year = parseInt(d.substring(0, d.indexOf(",")));
      var month = parseInt(d.substring(d.indexOf(",")+1, d.lastIndexOf(",")));
      var date = parseInt(d.substring(d.lastIndexOf(",")+1));
      var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
      var isleapyear = IsLeapYear(year);
      var dateth = 0;
      var monthday = 0;
      if (isleapyear) {
        if (month == 1) {
          dateth = date;
        } else {
          for (var i = 0; i < month - 1; i++) {
            monthday += months[i];
          }
          dateth = monthday + date;
        }
      } else {
        if (month == 1) {
          dateth = date;
        } else {
          for (var i = 0; i < month - 1; i++) {
            monthday += months[i];
          }
          dateth = monthday + date + 1;
        }
      }
      return dateth;
    }

    alert("该日期是所在年份的第" + DayOfYear(d) + "天");

    function IsLeapYear(year) {
      if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        return true;
      } else {
        return false;
      }
    }
  </script>
</body>
</html>
